package top.lishuoboy.redis.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.lishuoboy.dependency.base.json.HuJsonUtil;

/**
 * @author lishuoboy
 * @date 2022/3/14 15:11
 */
@Slf4j
@Configuration
public class RedissonConfig {
    @Bean
    public RedissonClient redissonClient() {
        // 1、配置类
        /**
         * 参数默认值
         * config==
         * {
         * 	"addressResolverGroupFactory":{},
         * 	"cleanUpKeysAmount":100,
         * 	"clusterConfig":false,
         * 	"keepPubSubOrder":true,
         * 	"lockWatchdogTimeout":30000,
         * 	"maxCleanUpDelay":1800,
         * 	"minCleanUpDelay":5,
         * 	"nettyHook":{},
         * 	"nettyThreads":32,
         * 	"referenceEnabled":true,
         * 	"reliableTopicWatchdogTimeout":600000,
         * 	"sentinelConfig":false,
         * 	"threads":16,
         * 	"transportMode":"NIO",
         * 	"useScriptCache":false,
         * 	"useThreadClassLoader":true
         * }
         */
        Config config = new Config();
        log.trace("config=={}", HuJsonUtil.toJsonStr(config));

        // 2、使用单服务器，也可以使用
        // config.useMasterSlaveServers();  // 主从
        // config.useSentinelServers();     // 哨兵
        // config.useClusterServers()       // 集群
        /**
         * 参数默认值
         * singleServerConfig==
         * {
         * 	"connectTimeout":10000,
         * 	"connectionMinimumIdleSize":24,
         * 	"connectionPoolSize":64,
         * 	"database":0,
         * 	"dnsMonitoringInterval":5000,
         * 	"idleConnectionTimeout":10000,
         * 	"keepAlive":false,
         * 	"nameMapper":{},
         * 	"pingConnectionInterval":30000,
         * 	"retryAttempts":3,
         * 	"retryInterval":1500,
         * 	"sslEnableEndpointIdentification":true,
         * 	"sslProvider":"JDK",
         * 	"subscriptionConnectionMinimumIdleSize":1,
         * 	"subscriptionConnectionPoolSize":50,
         * 	"subscriptionsPerConnection":5,
         * 	"tcpNoDelay":true,
         * 	"timeout":3000
         * }
         */
        SingleServerConfig singleServerConfig = config.useSingleServer();
        log.trace("singleServerConfig=={}", HuJsonUtil.toJsonStr(singleServerConfig));

        // 3、添加redis地址
        singleServerConfig.setAddress("redis://localhost:6379");

        // 4、创建客户端
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }

    /** 联锁 redis 客户端1 */
    @Bean
    public RedissonClient redissonClient1() {
        // 1、配置类
        Config config = new Config();
        // 2、使用单服务器，也可以使用
        SingleServerConfig singleServerConfig = config.useSingleServer();
        // 3、添加redis地址
        singleServerConfig.setAddress("redis://localhost:6379");
        // 4、创建客户端
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }

    /** 联锁 redis 客户端2 */
//    @Bean
//    public RedissonClient redissonClient2() {
//        Config config = new Config();
//        SingleServerConfig singleServerConfig = config.useSingleServer();
//        singleServerConfig.setAddress("redis://localhost:6380");
//        RedissonClient redissonClient = Redisson.create(config);
//        return redissonClient;
//    }

    /** 联锁 redis 客户端3 */
//    @Bean
//    public RedissonClient redissonClient3() {
//        Config config = new Config();
//        SingleServerConfig singleServerConfig = config.useSingleServer();
//        singleServerConfig.setAddress("redis://localhost:6381");
//        RedissonClient redissonClient = Redisson.create(config);
//        return redissonClient;
//    }
}